home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / event_post.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  16.0 KB  |  518 lines

  1. #ifndef lint
  2. static char SccsId[]= "@(#)event_post.c    V1.14    3/13/95";
  3. #endif
  4. /*
  5. |    file name - event_post.c
  6. |===================================================================
  7. |
  8. |    Example program that shows how to use event request post
  9. |       routine 'VUerBoundaryEventPost()' to detect the rectangle
  10. |       edge and object edge events. It also shows how to include
  11. |       the V_BUTTONPRESS and V_KEYPRESS winevents when posting
  12. |       the event request.
  13. |
  14. |       This example let you choose one of five sample event request
  15. |       post methods by a DV-Draw created menu object. Three different
  16. |       polygons with different attributes are used to illustrate the
  17. |       functionality of each event request post method.
  18. |
  19. |       Type 'q' or 'Q' to quit this program.
  20. |
  21. |===================================================================
  22. */
  23. #include <windows.h>
  24. #include "std.h"
  25. #include "dvstd.h"
  26. #include "VOstd.h"
  27. #include "dvtools.h"
  28. #include "dvinteract.h"
  29. #include "Tfundecl.h"
  30. #include "VOfundecl.h"
  31. #include "VUerfundecl.h"
  32. //#include "GRkeysym.h"           /* Includes GRkeysymdef.h */
  33.  
  34. /* Define the different event request post method */
  35. #define RECT_EDGE            1   /* Base on obj's bounding box */
  36. #define OBJECT_EDGE            2   /* Base on obj's edge */
  37. #define RECT_EDGE_BUTTON_PRESS        3   /* Bounding box + mouse button*/
  38. #define OBJECT_EDGE_KEY_PRESS        4   /* Obj edge + key press */
  39. #define RECT_EDGE_BUTTON_KEY_PRESS    5   /* Bounding box + mouse Button
  40.                                                     | + key press */
  41.  
  42. /* Define the characteristics of the three polygons */
  43. #define NUM_OF_TEST_POLY    3
  44. #define EDGE_POLY        0
  45. #define TRANSPARENT_POLY    1
  46. #define FILL_POLY        2
  47.  
  48. #define VCAST (INT (*)(OBJECT, EVENT_REQUEST, INT, OBJECT, ADDRESS))
  49.  
  50. /* Information to be passed on to the service routine HandleInput() */
  51. typedef struct
  52. {
  53.   DRAWPORT drawport;
  54.   OBJECT poly[NUM_OF_TEST_POLY];/* Array for the three polygons */
  55.   OBJECT mark[NUM_OF_TEST_POLY];/* Mark object for each polygon */
  56.   float *data_ptr;
  57.   int post_mode;
  58. } INFO;
  59.  
  60.  
  61. /* Define the state for the polygons' mark objects */
  62. LOCAL int init_state[]= {EDGE, EDGE, EDGE};
  63. LOCAL int mark_state[]= {EDGE_WITH_FILL, EDGE_WITH_FILL, EDGE_WITH_FILL};
  64.  
  65. #define BUTTON_1    1   /* default select key */
  66. #define BUTTON_2    2
  67. #define BUTTON_3    3
  68. #define XK_a            0x061
  69. #define XK_s            0x073
  70. #define XK_z            0x07a
  71. #define XK_b            0x062
  72. #define XK_space        0x020
  73. #define XK_semicolon    0x03b
  74.  
  75.  
  76. /* Define the buttonsyms */
  77. LOCAL ULONG buttonsyms[]= {BUTTON_1, BUTTON_2, BUTTON_3, V_END_OF_LIST};
  78.  
  79. /* Define the keysyms, keysyms are defined in GRkeysymdef.h
  80.  | Define key string 's z' in keysyms_1 and 'ab;' in keysyms_2 */
  81. LOCAL ULONG keysyms_1[] = {XK_s, XK_space, XK_z, V_END_OF_LIST};
  82. LOCAL ULONG keysyms_2[] = {XK_a, XK_b, XK_semicolon, V_END_OF_LIST};
  83.  
  84. LOCAL OBJECT bound_box[NUM_OF_TEST_POLY];       /* Bounding boxes for polygons */
  85.  
  86. /* Functions defined in event_post.c */
  87. /***************** Begin Function Declarations *************/
  88. LOCAL  int PostEvent V_P_((OBJECT client, EVENT_REQUEST er, int label, 
  89.                            OBJECT loc, ADDRESS args));
  90. LOCAL  int DrawMark V_P_((OBJECT client, EVENT_REQUEST er, int label, 
  91.                           OBJECT loc, ADDRESS args));
  92. LOCAL  int ClearMark V_P_((OBJECT client, EVENT_REQUEST er, int label, 
  93.                            OBJECT loc, ADDRESS args));
  94. LOCAL  void GetBoundBox V_P_((INFO *info, OBJECT bound_box[]));
  95. LOCAL  void RectEdge V_P_((INFO *info));
  96. LOCAL  void ObjectEdge V_P_((INFO *info));
  97. LOCAL  void RectEdge_ButtonPress V_P_((INFO *info));
  98. LOCAL  void ObjectEdge_KeyPress V_P_((INFO *info));
  99. LOCAL  void RectEdge_ButtonKeyPress V_P_((INFO *info));
  100. /***************** End Function Declarations *************/
  101.  
  102. /*
  103.  *   MAIN PROGRAM
  104.  */
  105. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  106.                      LPSTR lpCmdLine, int nCmdShow )
  107. {
  108.   INT argc=0;
  109.   CHAR **argv;
  110.   /* argv[1] - device name (default is DVDEVICE) */
  111.  
  112.   OBJECT screen, location;
  113.   VIEW view;
  114.   int key;
  115.  
  116.   /* Input object related variables */
  117.   OBJECT menu;
  118.   ADDRESS *vdplist;
  119.   INFO info;
  120.   int numvars;
  121.   LOCAL RECTANGLE whole_world = {XMIN, YMIN, XMAX, YMAX};
  122.  
  123.   make_argv(&argc,&argv,GetCommandLine());
  124.   /* 
  125.    *   Initialize DV-Tools, possibly using user's search path
  126.    */
  127.   TInit ((char *) NULL, (char *) NULL);  /* Use config var DVPATH */
  128.  
  129.   /*
  130.    *   Open the indicated device, load the view, rebind all its variables
  131.    *     to a single data value, and set up the drawport.
  132.    */
  133.   if (argc > 1)
  134.     screen = TscOpenSet (argv[1], (char *) NULL,
  135.                          V_X_EXPOSURE_BLOCK, YES, V_END_OF_LIST);
  136.   else
  137.     screen = TscOpenSet ((char *) NULL, (char *) NULL,
  138.                          V_X_EXPOSURE_BLOCK, YES, V_END_OF_LIST);
  139.   if (!screen)
  140.     {
  141.       printf ("Must specify device on command line or");
  142.       printf (" in DataViews configuration file.\n");
  143.       exit (EXIT_ERR);
  144.     }
  145.  
  146.   /* Load the static View and extract object info */
  147.   view = TviLoad ("event_post.v");
  148.   info.poly[EDGE_POLY] = TdrGetNamedObject (TviGetDrawing (view),
  149.                                             "py_edge.obj");
  150.   info.poly[TRANSPARENT_POLY] = TdrGetNamedObject (TviGetDrawing (view),
  151.                                                    "py_trans.obj");
  152.   info.poly[FILL_POLY] = TdrGetNamedObject (TviGetDrawing (view),
  153.                                             "py_fill.obj");
  154.   info.mark[EDGE_POLY] = TdrGetNamedObject (TviGetDrawing (view),
  155.                                             "mark_edge.obj");
  156.   info.mark[TRANSPARENT_POLY] = TdrGetNamedObject (TviGetDrawing (view),
  157.                                                    "mark_trans.obj");
  158.   info.mark[FILL_POLY] = TdrGetNamedObject (TviGetDrawing (view),
  159.                                             "mark_fill.obj");
  160.   info.drawport = TdpCreateStretch (screen, view, (RECTANGLE *) NULL,
  161.                                     &whole_world);
  162.  
  163.   /* Get bounding box for each poly */
  164.   GetBoundBox (&info, bound_box);
  165.  
  166.   /* Get the menu input objects and its variable */
  167.   menu = TdrGetNamedObject (TviGetDrawing (view), "menu_input.obj");
  168.   VOinGetVarList (menu, &vdplist, &numvars);
  169.   info.data_ptr = (float *) TdsvGetBuffer (TvdGetDataSourceVariable (vdplist[0]));
  170.  
  171.   /* Post service result request for the menu input object.
  172.    | Key bindings are defined in the view */
  173.   VUerServiceResultPost ((OBJECT) 1, (VUERFCNFUNPTR)PostEvent, 
  174.              (ADDRESS) & info,
  175.                          (int)sizeof (info), menu, (int) INPUT_DONE, (int)0);
  176.  
  177.   /* Draw the view */
  178.   TscErase (screen);
  179.   TdpDraw (info.drawport);
  180.  
  181.   /*
  182.    *   Continually poll the cursor checking for button presses that
  183.    *   indicate invocation of one of the event request methods.
  184.    */
  185.  
  186.   FOREVER
  187.   {
  188.     location = TloPoll (LOC_POLL);
  189.     key = VOloKey (location);
  190.     if (key == 'q' || key == 'Q')
  191.       break;
  192.  
  193.     /* Let the Event Handler deal with inputs */
  194.     VUerHandleLocEvent (location);
  195.   }
  196.  
  197.   /*
  198.    *   Do cleanup stuff --  erase the screen, free dynamic memory, close
  199.    *     the display device, and perform final DataViews cleanup.
  200.    */
  201.   VUerClearAll ((OBJECT) 1);
  202.   TdpDestroy (info.drawport);
  203.   TviDestroy (view);
  204.   TscCloseCurrentScreen ();
  205.   TTerminate ();
  206.   return EXIT_OK;
  207. }
  208.  
  209. /*
  210.  * PostEvent
  211.  *
  212.  * Service routine for menu input object's service result request which
  213.  * sets up the chosen event request post method for the polygons.
  214.  *
  215.  */
  216. /*ARGSUSED*/
  217. LOCAL int 
  218. PostEvent (client, er, label, loc, args)
  219.      OBJECT client;
  220.      EVENT_REQUEST er;
  221.      int label;
  222.      OBJECT loc;
  223.      ADDRESS args;
  224. {
  225.  
  226.   INFO *info = (INFO *)args;
  227.  
  228.   info->post_mode = (int) (*info->data_ptr);
  229.  
  230.   switch (info->post_mode)
  231.     {
  232.     case RECT_EDGE:
  233.       RectEdge (info);
  234.       break;
  235.     case OBJECT_EDGE:
  236.       ObjectEdge (info);
  237.       break;
  238.     case RECT_EDGE_BUTTON_PRESS:
  239.       RectEdge_ButtonPress (info);
  240.       break;
  241.     case OBJECT_EDGE_KEY_PRESS:
  242.       ObjectEdge_KeyPress (info);
  243.       break;
  244.     case RECT_EDGE_BUTTON_KEY_PRESS:
  245.       RectEdge_ButtonKeyPress (info);
  246.       break;
  247.     }
  248.  
  249.   return (int) INPUT_USED;
  250. }
  251.  
  252. /*
  253.  * DrawMark
  254.  *
  255.  * Fill the mark object of a selected polygon
  256.  *
  257.  */
  258. /*ARGSUSED*/
  259. LOCAL int 
  260. DrawMark (client, er, label, loc, args)
  261.      OBJECT client;
  262.      EVENT_REQUEST er;
  263.      int label;
  264.      OBJECT loc;
  265.      ADDRESS args;
  266. {
  267.   ATTRIBUTES attr;
  268.   INFO *info = (INFO *)args;
  269.  
  270.   TdpDrawObject (info->drawport, bound_box[label]);
  271.  
  272.   VOobAtGet (info->mark[label], &attr);
  273.   if (attr.fill_status != mark_state[label])
  274.     {
  275.       TdpEraseObject (info->drawport, info->mark[label]);
  276.       attr.fill_status = mark_state[label];
  277.  
  278.       VOobAtSet (info->mark[label], &attr);
  279.       TdpDrawObject (info->drawport, info->mark[label]);
  280.     }
  281.  
  282.   return (int) INPUT_USED;
  283. }
  284.  
  285. /*
  286.  * ClearMark
  287.  *
  288.  * Clear the mark object of a selected polygon is unselected
  289.  *
  290.  */
  291. /*ARGSUSED*/
  292. LOCAL int 
  293. ClearMark (client, er, label, loc, args)
  294.      OBJECT client;
  295.      EVENT_REQUEST er;
  296.      int label;
  297.      OBJECT loc;
  298.      ADDRESS args;
  299. {
  300.   ATTRIBUTES attr;
  301.   INFO *info = (INFO *)args;
  302.  
  303.   TdpEraseObject (info->drawport, bound_box[label]);
  304.  
  305.   VOobAtGet (info->mark[label], &attr);
  306.   if (attr.fill_status != init_state[label])
  307.     {
  308.       TdpEraseObject (info->drawport, info->mark[label]);
  309.       attr.fill_status = init_state[label];
  310.  
  311.       VOobAtSet (info->mark[label], &attr);
  312.       TdpDrawObject (info->drawport, info->mark[label]);
  313.     }
  314.  
  315.   return (int) INPUT_USED;
  316. }
  317.  
  318. /*
  319.  * GetBoundBox
  320.  *
  321.  * Get each polygon's bounding box
  322.  *
  323.  */
  324. LOCAL void 
  325. GetBoundBox (info, bound_box)
  326.      INFO *info;
  327.      OBJECT bound_box[];
  328. {
  329.   RECTANGLE wvp, svp_delta, svp;
  330.   ATTRIBUTES attr;
  331.   OBJECT pt_obj1, pt_obj2;
  332.   int i;
  333.  
  334.   for (i = 0; i < NUM_OF_TEST_POLY; i++)
  335.     {
  336.       VOobBox (info->poly[i], &wvp, &svp_delta);
  337.  
  338.       TdpWorldToScreen (info->drawport, &wvp.ll, &svp.ll);
  339.       TdpWorldToScreen (info->drawport, &wvp.ur, &svp.ur);
  340.  
  341.       VOobAtGet (info->poly[i], &attr);
  342.       attr.line_type = (char) 2;/* Any line type except SOLID_LINE(1) */
  343.       attr.fill_status = EDGE;
  344.       attr.line_width = (char) 1;
  345.  
  346.       pt_obj1 = VOptCreate (WORLD_COORDINATES, wvp.ll.x, wvp.ll.y, (OBJECT) 0);
  347.       pt_obj2 = VOptCreate (WORLD_COORDINATES, wvp.ur.x, wvp.ur.y, (OBJECT) 0);
  348.  
  349.       bound_box[i] = VOreCreate (pt_obj1, pt_obj2, &attr);
  350.     }
  351. }
  352.  
  353. /*
  354.  * RectEdge
  355.  *
  356.  * Post the event request to select an object base on the position of
  357.  * the mouse pointer and the edge detection of the object's bounding box
  358.  *
  359.  */
  360. LOCAL void 
  361. RectEdge (info)
  362.      INFO *info;
  363. {
  364.   RECTANGLE wvp, svp, svp_delta;
  365.   int i;
  366.  
  367.   VUerClearAll ((OBJECT) 2);
  368.  
  369.   for (i = 0; i < NUM_OF_TEST_POLY; i++)
  370.     {
  371.       VOobBox (info->poly[i], &wvp, &svp_delta);
  372.       TdpWorldToScreen (info->drawport, &wvp.ur, &svp.ur);
  373.       TdpWorldToScreen (info->drawport, &wvp.ll, &svp.ll);
  374.       VOuVpSort (&svp);
  375.       VUerBoundaryEventPost ((OBJECT) 2, VCAST DrawMark, (ADDRESS) info,
  376.                              (int)sizeof (INFO), i, VUER_POS_EVENT,
  377.                              &svp, (int) (int)V_INSIDE);
  378.       VUerBoundaryEventPost ((OBJECT) 2, VCAST ClearMark, (ADDRESS) info,
  379.                              (int)sizeof (INFO), i, VUER_POS_EVENT,
  380.                              &svp, (int) (int)V_OUTSIDE);
  381.     } 
  382. }
  383.  
  384. /*
  385.  * ObjectEdge
  386.  *
  387.  * Post the event request to select an object base on the position of
  388.  * the mouse pointer and the edge detection of the object's edges
  389.  *
  390.  */
  391. LOCAL void 
  392. ObjectEdge (info)
  393.      INFO *info;
  394. {
  395.   int i;
  396.  
  397.   VUerClearAll ((OBJECT) 2);
  398.  
  399.   for (i = 0; i < NUM_OF_TEST_POLY; i++)
  400.     {
  401.       VUerBoundaryEventPost ((OBJECT) 2, VCAST DrawMark, (ADDRESS) info,
  402.                              (int)sizeof (INFO), i, VUER_OPOS_EVENT, info->poly[i],
  403.                              (OBJECT)TdpGetXform (info->drawport, DR_TO_SCREEN),
  404.                              (int)V_INSIDE);
  405.       VUerBoundaryEventPost ((OBJECT) 2, VCAST ClearMark, (ADDRESS) info,
  406.                              (int)sizeof (INFO), i, VUER_OPOS_EVENT, info->poly[i],
  407.                              (OBJECT)TdpGetXform (info->drawport, DR_TO_SCREEN),
  408.                              (int)V_OUTSIDE);
  409.     }
  410. }
  411.  
  412. /*
  413.  * RectEdge_ButtonPress
  414.  *
  415.  * Post the event request to select an object base on
  416.  *  1. the position of the mouse pointer, and
  417.  *  2. a mouse button press, and
  418.  *  3. the edge detection of the object's bounding box.
  419.  *
  420.  */
  421. LOCAL void 
  422. RectEdge_ButtonPress (info)
  423.      INFO *info;
  424. {
  425.   RECTANGLE wvp, svp, svp_delta;
  426.   int i;
  427.  
  428.   VUerClearAll ((OBJECT) 2);
  429.  
  430.   for (i = 0; i < NUM_OF_TEST_POLY; i++)
  431.     {
  432.       VOobBox (info->poly[i], &wvp, &svp_delta);
  433.       TdpWorldToScreen (info->drawport, &wvp.ur, &svp.ur);
  434.       TdpWorldToScreen (info->drawport, &wvp.ll, &svp.ll);
  435.       VOuVpSort (&svp);
  436.       VUerBoundaryEventPost ((OBJECT) 2, VCAST DrawMark, (ADDRESS) info,
  437.                              (int)sizeof (INFO), i, VUER_BRE_EVENT, V_BUTTONPRESS,
  438.                              buttonsyms, &svp, (int)V_INSIDE);
  439.       VUerBoundaryEventPost ((OBJECT) 2, VCAST ClearMark, (ADDRESS) info,
  440.                              (int)sizeof (INFO), i, VUER_BRE_EVENT, V_BUTTONPRESS,
  441.                              buttonsyms, &svp, (int)V_OUTSIDE);
  442.     }
  443. }
  444.  
  445. /*
  446.  * ObjectEdge_KeyPress
  447.  *
  448.  * Post the event request to select an object base on
  449.  *  1. the position of the mouse pointer, and
  450.  *  2. a key press of keys defined in keysyms_1 array, and
  451.  *  3. the edge detection of the object's edges.
  452.  *
  453.  */
  454. LOCAL void 
  455. ObjectEdge_KeyPress (info)
  456.      INFO *info;
  457. {
  458.   int i;
  459.  
  460.   VUerClearAll ((OBJECT) 2);
  461.  
  462.   for (i = 0; i < NUM_OF_TEST_POLY; i++)
  463.     {
  464.       VUerBoundaryEventPost ((OBJECT) 2, VCAST DrawMark, (ADDRESS) info,
  465.                              (int)sizeof (INFO), i, VUER_DOE_EVENT, V_KEYPRESS,
  466.                              keysyms_1, info->poly[i],
  467.                              (OBJECT)TdpGetXform (info->drawport, DR_TO_SCREEN),
  468.                              (int)V_INSIDE);
  469.       VUerBoundaryEventPost ((OBJECT) 2, VCAST ClearMark, (ADDRESS) info,
  470.                              (int)sizeof (INFO), i, VUER_DOE_EVENT, V_KEYPRESS,
  471.                              keysyms_1, info->poly[i],
  472.                              (OBJECT)TdpGetXform (info->drawport, DR_TO_SCREEN),
  473.                              (int)V_OUTSIDE);
  474.     }
  475. }
  476.  
  477.  
  478. /*
  479.  * RectEdge_ButtonKeyPress
  480.  *
  481.  * Post the event request to select an object base on
  482.  *  1. the position of the mouse pointer, and
  483.  *  2. a mouse button press or a key press of keys defined in
  484.  *     keysyms_2 array, and
  485.  *  3. the edge detection of the object's bounding box.
  486.  *
  487.  */
  488. LOCAL void 
  489. RectEdge_ButtonKeyPress (info)
  490.      INFO *info;
  491. {
  492.   RECTANGLE wvp, svp, svp_delta;
  493.   int i;
  494.  
  495.   VUerClearAll ((OBJECT) 2);
  496.  
  497.   for (i = 0; i < NUM_OF_TEST_POLY; i++)
  498.     {
  499.       VOobBox (info->poly[i], &wvp, &svp_delta);
  500.       TdpWorldToScreen (info->drawport, &wvp.ur, &svp.ur);
  501.       TdpWorldToScreen (info->drawport, &wvp.ll, &svp.ll);
  502.       VOuVpSort (&svp);
  503.       VUerBoundaryEventPost ((OBJECT) 2, VCAST DrawMark, (ADDRESS) info,
  504.                              (int)sizeof (INFO), i, VUER_BRE_EVENT, V_BUTTONPRESS,
  505.                              buttonsyms, &svp, (int)V_INSIDE);
  506.       VUerBoundaryEventPost ((OBJECT) 2, VCAST DrawMark, (ADDRESS) info,
  507.                              (int)sizeof (INFO), i, VUER_BRE_EVENT, V_KEYPRESS,
  508.                              keysyms_2, &svp, (int)V_INSIDE);
  509.  
  510.       VUerBoundaryEventPost ((OBJECT) 2, VCAST ClearMark, (ADDRESS) info,
  511.                              (int)sizeof (INFO), i, VUER_BRE_EVENT, V_BUTTONPRESS,
  512.                              buttonsyms, &svp, (int)V_OUTSIDE);
  513.       VUerBoundaryEventPost ((OBJECT) 2, VCAST ClearMark, (ADDRESS) info,
  514.                              (int)sizeof (INFO), i, VUER_BRE_EVENT, V_KEYPRESS,
  515.                              keysyms_2, &svp, (int)V_OUTSIDE);
  516.     }
  517. }
  518.